$(function () {
    //新增规格
    $("[name='btn_save_new_pbc']").click(function () {
        var form = window.form_new_pbc;
        var pcount = form.pcount.value;
        var state = form.state.value;
        if (isNaN(pcount)) {
            bootbox.balert("每箱数量输入有误");
            return;
        }
        if(pcount <= 0){
        	bootbox.balert("每箱数量必须是大于0的数字");
            return;
        }
        if(!isInt(pcount)){
        	bootbox.balert("每箱数量必须是整数");
            return;
        }
        pbc = {pcount: pcount, state: state};

        $.postJsonBody("/pbc", pbc, function (re) {
            // tipper.success("已保存，请刷新页面");
            // $("#md_new_pbc").modal("hide");
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });


    //编辑规格
    $("[name='btn_save_edit_pbc']").click(function () {
        var form = window.form_edit_pbc;
        var id = form.id.value;
        var pcount = form.pcount.value;
        var state = form.state.value;

        if (isNaN(pcount)) {
            bootbox.balert("每箱数量输入有误");
            return;
        }
        if(pcount <= 0){
        	bootbox.balert("每箱数量必须是大于0的数字");
            return;
        }
        if(!isInt(pcount)){
        	bootbox.balert("每箱数量必须是整数");
            return;
        }
        var pbc = {id: id, pcount: pcount, state: state};
        $.putJsonBody("/pbc/" + id, pbc, function (re) {
            // tipper.success("已更新，请刷新页面");
            // $("#md_edit_pbc").modal("hide");
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });

    //删除规格
    $("[name='btn_delete_pbc']").click(function () {
        var pbcId = $(this).data("pbc-id");
        bootbox.bconfirm("确定要删除这个规格吗？", function (re) {
            if (re) {
                $.delete("/pbc/" + pbcId, function (re) {
                    if (re.data) {
                        // tipper.success("删除成功，请刷新页面");
                    	window.location.href=window.location.href; 
                    	window.location.reload; 
                    } else {
                        tipper.danger("删除失败，请重试");
                    }
                });
            }
        });
    });


    // 规格详情
    $('#md_edit_pbc').on('show.bs.modal', function (event) {
        var _e = $(event.relatedTarget);
        var id = _e.data("pbc-id");
        var modal = $(this);
        var form = window.form_edit_pbc;
        $.get("/pbc/" + id, function (re) {
            var pbc = re.data;
            form.id.value = pbc.id;
            form.pcount.value = pbc.pcount;
            $(form.state).find("option").attr("selected", false);
            $(form.state).find("option[value='" + pbc.state + "']").attr("selected", true);
        });
    });
    
  //查询条件重置
    $("#search_reset").click(function(){
    	$("input").val("");
        $("select").val("");
    });
    
});

function isInt(str)
{
    var g=/^-?\d+$/;
    return g.test(str);
}